package com.example.springboot;

import com.example.springboot.entity.Permission;
import com.example.springboot.entity.Role;
import com.example.springboot.service.PermissionService;
import com.example.springboot.service.RoleService;
import com.example.springboot.service.UserService;
import com.example.springboot.service.impl.PermissionServiceImpl;
import com.example.springboot.service.impl.RoleServiceImpl;
import com.example.springboot.service.impl.UserServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.example.springboot.entity.User;

/**
 * Created by tang on 2018/1/3.
 */
public class TestShiro {

    public void test(String configPath) {
        //1、获取SecurityManager工厂，此处使用Ini配置文件初始化SecurityManager
        Factory<SecurityManager> factory =
                new IniSecurityManagerFactory("classpath:" + configPath);

        //2、得到SecurityManager实例 并绑定给SecurityUtils
        org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);

        //3、得到Subject及创建用户名/密码身份验证Token（即用户身份/凭证）
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");

        try {
            //4、登录，即身份验证
            subject.login(token);
        } catch (AuthenticationException e) {
            //5、身份验证失败
        }
        PrincipalCollection principalCollection = subject.getPrincipals();
        System.out.println(principalCollection);

        System.out.println(subject.isAuthenticated());
        Assert.assertEquals(true, subject.isAuthenticated()); //断言用户已经登录

        //6、退出
        subject.logout();
    }


    @org.junit.Test
    public void testJdbcRealm() {
        // test("shiro-jdbc-realm.ini");
        test("shiro-user-realm.ini");

    }


    @Test
    public void setUp() {
        UserService userService = new UserServiceImpl();
        RoleService roleService = new RoleServiceImpl();
        PermissionService permissionService = new PermissionServiceImpl();
//
//       User user=new User();

  /*      Role role1=new Role();
        role1.setAvailable(true);
        role1.setRole("admin");
        role1.setDescription("管理员");

        Role role2=new Role();
        role2.setRole("user");
        role2.setAvailable(true);
        role2.setDescription("用户");

        roleService.createRole(role1);
        roleService.createRole(role2);*/


   /*     Permission p1 = new Permission("user:create", "用户模块新增", Boolean.TRUE);
        Permission p2 = new Permission("user:update", "用户模块修改", Boolean.TRUE);
        Permission p3 = new Permission("menu:create", "菜单模块新增", Boolean.TRUE);
        permissionService.createPermission(p1);
        permissionService.createPermission(p2);

        permissionService.createPermission(p3);
*/


 //  roleService.correlationPermissions(1l,1l,2l,3l);

   //     roleService.correlationPermissions(2l,1l,2l);
//     String password="123";
//      User  u1 = new User("zhang", password);
//      User  u2 = new User("li", password);
//       User u3 = new User("wu", password);
//       User u4 = new User("wang", password);
//
//        userService.createUser(u1);
//        userService.createUser(u2);
//        userService.createUser(u3);
//        userService.createUser(u4);


        userService.correlationRoles(1l,1l);
        userService.correlationRoles(2l,2l);
        userService.correlationRoles(3l,2l);
        userService.correlationRoles(4l,2l);



    }


}
